package com.nosnow.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nosnow.edu.entity.EduChapter;
import com.nosnow.edu.entity.EduVideo;
import com.nosnow.edu.entity.chapter.ChapterVo;
import com.nosnow.edu.entity.chapter.VideoVo;
import com.nosnow.edu.mapper.EduChapterMapper;
import com.nosnow.edu.mapper.EduVideoMapper;
import com.nosnow.edu.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nosnow.handler.SnowflakeException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author bingheng
 * @since 2020-04-04
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoMapper eduVideoMapper;

    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {
        // 1. 根据课程Id查询课程里面所有的章节
        QueryWrapper<EduChapter> wrapperEduChapter = new QueryWrapper<>();
        wrapperEduChapter.eq("course_id", courseId);
        List<EduChapter> eduChapters = baseMapper.selectList(wrapperEduChapter);

        // 2. 根据课程Id查询课程中所有的小节
        QueryWrapper<EduVideo> wrapperVideo = new QueryWrapper<>();
        wrapperVideo.eq("course_id", courseId);
        List<EduVideo> eduVideos = eduVideoMapper.selectList(wrapperVideo);

        // 3. 创建list集合，用于封装最终的数据
        List<ChapterVo> resultList = new ArrayList<>();
        if(eduChapters != null && !eduChapters.isEmpty()) {

            // 3.1 章节
            eduChapters.forEach(o -> {
                ChapterVo chapterVo = new ChapterVo();
                BeanUtils.copyProperties(o, chapterVo);
                resultList.add(chapterVo);

                // 3.2 小节
                List<VideoVo> videoVos = new ArrayList<>();
                if(eduVideos != null && !eduVideos.isEmpty()) {
                    eduVideos.forEach(t -> {
                        if(t.getChapterId().equals(o.getId())) {
                            VideoVo videoVo = new VideoVo();
                            BeanUtils.copyProperties(t, videoVo);
                            videoVos.add(videoVo);
                        }
                    });
                }
                chapterVo.setChildren(videoVos);
            });
        }
        return resultList;
    }

    @Override
    public Boolean deleteChapter(String chapterId) {
        // 1. chapterId 查询小节，如果小节存在，则不删除数据
        QueryWrapper<EduVideo> wrapperEduVideo = new QueryWrapper<>();
        wrapperEduVideo.eq("chapter_id", chapterId);
        Integer num = eduVideoMapper.selectCount(wrapperEduVideo);

        // 2. 判断该章节是否存在小节，不存在则删除
        if(num > 0) {
            throw new SnowflakeException(20001, "此章节中存在小节，不能删除该章节");
        } else {
            return baseMapper.deleteById(chapterId) > 0;
        }
    }
}
